package com.yugao.fintech.antelope.auth.controller;

import com.yugao.fintech.antelope.auth.model.rqrs.CaptchaCheckReq;
import com.yugao.fintech.antelope.auth.model.rqrs.CaptchaResp;
import com.yugao.fintech.antelope.auth.service.CaptchaService;
import com.yugao.fintech.antelope.base.security.annotation.Anonymous;
import com.yugao.fintech.framework.core.response.Resp;
import com.yugao.fintech.framework.samples.ratelimit.LimitTypeEnum;
import com.yugao.fintech.framework.samples.ratelimit.RateLimit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

/**
 * 验证码管理
 */
@Slf4j
@RestController
@RequestMapping("/captcha")
public class CaptchaController {
    @Autowired
    private CaptchaService captchaService;

    @GetMapping("/image")
    @RateLimit(key = "rate_limit::captcha", time = 20, count = 10, limitType = LimitTypeEnum.IP, msg = "操作过于频繁, 请 ${remainingTime} 后再次尝试")
    @Anonymous
    public Resp<CaptchaResp> createCaptcha() {
        return captchaService.createCaptcha();
    }

    @PostMapping("/check")
    public Resp<Boolean> checkCaptcha(@Validated @RequestBody CaptchaCheckReq req) {
        return Resp.ok(captchaService.checkCaptcha(req.getImageCaptcha(), req.getUuid()));
    }

    @PostMapping("/delete")
    public Resp<?> deleteCaptcha(@RequestParam("uuid") String uuid) {
        captchaService.deleteCaptcha(uuid);
        return Resp.ok();
    }
}
